﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MpSoft.Library.BUSS.Customers;
using MpSoft.Library.DTO;

namespace MpSoft.Web.AdminCP.Controls.Customer
{
    public partial class CustomerEdit : System.Web.UI.UserControl
    {
        #region Var

        private int customerID = -1;
        
        #endregion

        #region Method

        private void BindControl()
        {
            if (customerID > 0)
            {
                var obj = CustomerBUSS.Customer_ByID(customerID);
                if (obj == null)
                {
                    ShowChangePass = false;
                    return;
                }
                ShowChangePass = true;
                txtUserName.Text = obj.Username;
                txtEmail.Text = obj.Email;
                chkIsActive.Checked = obj.Active;
                chkIsAdmin.Checked = obj.IsAdmin;
            }
        }

        private int Save()
        {
            Library.DTO.Customer obj = null;
            if (customerID > 0)
                obj = CustomerBUSS.Customer_ByID(customerID);
            if (obj == null)
                obj = new Library.DTO.Customer();
            obj.Email = txtEmail.Text.Trim();
            obj.Username = txtUserName.Text.Trim();
            obj.IsAdmin = chkIsAdmin.Checked;
            obj.Active = chkIsActive.Checked;
            if (customerID > 0)
            {
                CustomerBUSS.Customer_Update(obj);
                this.ShowChangePass = true;
                return obj.CustomerID;
            }
            obj.PasswordHash = txtPassWord.Text.Trim();
            obj.RegistrationDate = DateTime.Now;
            if (!txtConfirmPass.Text.Trim().Equals(txtPassWord.Text.Trim()))
            {
                Message("Cập nhật không thành công, Mật khẩu bạn chưa nhập đúng!", 2);
                return -1;
            }
            var obj2 = CustomerBUSS.Customer_List(-1, !string.IsNullOrEmpty(obj.Username) ? obj.Username : null,
                !string.IsNullOrEmpty(obj.Email) ? obj.Email : null);
            if (obj2 != null)
            {
                Message("Email hoặc Username này đã tồn tại. Yêu cầu sử dụng email hay Username khác!", 2);
                return -1;
            }
            return CustomerBUSS.Customer_Add(obj);
        }

        private void Message(string message,int type)
        {
            switch (type)
            {
                case 1:
                    MpSoft.Basic.Utils.CommonHelper.ResetForm(this.Controls);
                    ltlMessage.Text = "<p class='message-success'><i class='glyphicon-ok'></i> " + message + "</p>";
                    break;
                case 2:
                    ltlMessage.Text = "<p class='message-warning'><i class='glyphicon-warning_sign'></i> " + message + "</p>";
                    break;
            }   
        }

        #endregion

        #region Event
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["itemid"] != null)
                int.TryParse(Request.QueryString["itemid"].ToString(), out customerID);
            if (!IsPostBack)
                BindControl();
        }

        protected void cmdSave_Click(object sender, EventArgs e)
        {
            if (Save() > 0)
                Message("Cập nhật thành công!", 1);
        }
        protected void cmdSaveExit_Click(object sender, EventArgs e)
        {
            if (Save() > 0)
                Response.Redirect("?mod=customer");
        }
        #endregion

        #region properties

        public bool ShowChangePass { set; get; }

        #endregion

        
    }
}